package com.oepm.web.controller;

import com.oepm.biz.AcademyBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.dao.entity.AcademyPo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/academy")
public class AcademyController {
    @Autowired
    private AcademyBiz acadenyBiz;

    /**
     * 查询学院及专业
     * @return
     */
    @GetMapping("/select")
    @RequiresRoles(value = {"1"})
    public Result selectAll(){
        List<AcademyPo> list = acadenyBiz.selectAll();
        Result result = new Result(Meta.SUCCESS,list);
        return result;
    }

    /**
     * 更新学院专业
     * @param academyPo
     * @return
     */
    @PostMapping("/update")
    @RequiresRoles(value = {"1"})
    public Result updateMajor(@RequestBody AcademyPo academyPo){
        int flag = acadenyBiz.updateMajor(academyPo);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 添加学院
     * @param academyPo
     * @return
     */
    @PostMapping("/insert")
    @RequiresRoles(value = {"1"})
    public Result insertAcademy(@RequestBody AcademyPo academyPo){
        int flag = acadenyBiz.insertAcademy(academyPo);
        Result result;
        if (flag>0){
            result = new Result(Meta.CREATED);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 删除学院
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiresRoles(value = {"1"})
    public Result deleteAcademy(@PathVariable String id){
        int flag = acadenyBiz.deleteAcademy(id);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }
}
